package com.token.sysorg.services;

import java.sql.ResultSet;

import com.token.sql.IntegrateSql;
import com.token.base.FuncUtil;
import com.token.base.Pl;
import com.token.sysorg.bean.FSysPost;
/**
 * 写组织架构-岗位
 * @author 代码创建
 */
public class FSysPostWrite{
	private String datetime = Pl.getCurDataTime();
	private String user;
	private int sysid = Pl.getSysId();
	public void writeData(FuncUtil F,IntegrateSql iSql) throws Exception {
		String keyvalue = F.getUrlQueryString("PostID");
		FSysPost dao = new FSysPost(iSql);
		dao.setInsertOrUpdateRs(keyvalue);
		user = F.getEffectiveUserName();
		if(dao.isUpdate()){
			dao.setSysEditTime(datetime); //修改时间
			dao.setSysEditUser(user); //修改人
			dao.setPostNumber(F.getUrlQueryString("PostNumber")); //公司ID
		}else{
			dao.setSysId(sysid); //租户ID //json.getIntValue("SysId")
			dao.setSysAddTime(datetime); //创建时间
			dao.setSysAddUser(user); //创建人
			dao.setSysEditTime(datetime); //修改时间
			dao.setSysEditUser(user); //修改人
			dao.setPostNumber(getComNum(dao.getTableName(), iSql)); //公司ID
		}
//		dao.setPostNumber(F.getUrlQueryString("PostNumber")); //岗位编号
		dao.setPostName(F.getUrlQueryString("PostName")); //岗位名称
		dao.setBenchmarkPost(F.getUrlQueryInteger("BenchmarkPost")); //基准岗位
		dao.executeUpdate();
		dao.close();

	}
	private String getComNum(String tablename, IntegrateSql iSql) throws Exception{
		System.out.print(tablename);
		String sql = "SELECT Max(PostNumber) AS UnitID  FROM " + tablename;
		ResultSet rs = iSql.executeQuery(sql);
		if(rs.next()){
			String num = rs.getString("UnitID");
			if(num == null || "NULL".equals(num.toUpperCase()) || "".equals(num)){
				return "G000001";
			}else{
				int id = Integer.parseInt(num.substring(1, num.length()));
				return buling("G", (id+1)+"");
			}
		}else{
			return "G000001";
		}
	}

	private String buling(String str1,String str2) {
		for(int i=str2.length();i<6;i++){
			str2 = "0" + str2;
		}
		return str1 + str2;
	}
}
